package com.example.carconsumer.carshiro.realm;

import com.alibaba.dubbo.config.annotation.Reference;

import com.example.carconsumer.carshiro.util.SpringBeanFactoryUtils;
import com.example.carinterface.pojo.Driver;
import com.example.carinterface.pojo.Login;
import com.example.carinterface.service.dl.LoginService;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;

@Component("l")
public class LoginRealm extends AuthorizingRealm {

    //

    @Reference
    private LoginService loginService;


    //    @Reference(check = false)
//    private RoleService roleService;
//    @Reference(check = false)
//    private UserService userService;
    public LoginRealm() {

        System.out.println("----------LoginRealm-------------");

//        if (loginService==null){
//
//            loginService= SpringBeanFactoryUtils.getBean(LoginService.class);
//        }


    }


    /**
     * 通过角色控制权限
     *
     * 暂时不考虑权限
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权111111");
        Object principal = principalCollection.getPrimaryPrincipal();
        Login login = null;
        if (principal instanceof Login)
            login = (Login) principal;
        else return null;

//
//        System.out.println(login);
//        String auth = login.getAuth();
//        Set<String> roleSet = new HashSet<>();
//
//        roleSet.add(auth);

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.addStringPermissions(roleSet);
        return info;


//        Login login = (Login) principalCollection.getPrimaryPrincipal();
//        List<Role> roleList = roleService.findByLoginId(login.getId());
//        Set<String> roleSet = new HashSet<>();
//
//        roleList.forEach(role -> {
//            roleSet.add(role.getName());
//        });
//
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleSet);
//
//        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("renzehn1111");
        System.out.println(loginService);
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        Login login = loginService.findLogin(username);

        Driver driver = loginService.findDriver(username);


        // 、如果用户不存在，可以抛出unknownaccountexception
        if (login == null && driver == null) {
            return null;//抛出异常 unknownaccountexception
        } else if (login != null) {
            //需要区别用户名
            System.out.println(login);
            ByteSource salt = ByteSource.Util.bytes(login.getUsername());
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(login, login.getPassword(), salt, getName());
            return info;
        } else {
            System.out.println(driver);
            ByteSource salt = ByteSource.Util.bytes(driver.getTel());
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(driver, driver.getPassword(), salt, getName());
            return info;
        }


    }
}
